package com
{
	import com.ajax.DBfun;
	import com.core.pubfunc;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.utils.setTimeout;
	
	import flexlib.containers.SuperTabNavigator;
	
	import mx.collections.ArrayCollection;
	import mx.core.FlexGlobals;
	import mx.events.ModuleEvent;
	import mx.modules.Module;
	import mx.modules.ModuleLoader;
	
	import org.puremvc.as3.multicore.utilities.fabrication.components.FlexModuleLoader;
	import org.puremvc.as3.multicore.utilities.fabrication.interfaces.INamedPipeFitting;
	
	import spark.components.Group;
	
	import swc.ct.ui.loading.preLoading;
	


	public class ModuleFunc
	{
		public function ModuleFunc()
		{
		}
		private static var group:Group;
		private static var load:preLoading;
		
		
		/**
		 * 获取某用户管辖权限范围内的部门列表
		 * 返回对象{parentorgid,orgname,orglevel,orgid,toporgid,orgseq}等
		 */
		public static function getMyManageOrg(operId:String):ArrayCollection{
			var sql:String="SELECT * FROM TABLE(CAST(WEBSTUDIOUSER.WY_GETUSERORG2("+operId+", 0) AS ORG_TAB))";
			return pubfunc.CreateDataGridArraydataUpper(DBfun.synExecSqlCmd(sql,null));
		}
		private static function createNewItem(obj:Object):XML {
			var org:XML;
			org=<orgitem />;
			org.@ORGNAME = obj.ORGNAME;        
			org.@PARENTORGID =obj.PARENTORGID;
			org.@ORGTYPEID=obj.ORGTYPEID;
			org.@ORGID =obj.ORGID;
			org.@TOPORGID=obj.TOPORGID;
			org.@OEGSEQ=obj.ORGSEQ;
			return org;
		}
		/**
		 *创建‘我的’机构树 
		 * @param orgAc
		 * @param flag 是否带岗位,true:显示岗位,false:不显示岗位
		 * @return 
		 * 
		 */		
		public static function creatMyOrgTree(tempAc:ArrayCollection,flag:Boolean=false):XML{
			var org:XML;
			var treenode:XMLList;
			for(var i:int=0; i < tempAc.length; i++)
			{   
				var obj:Object=tempAc.getItemAt(i);
				if(i==0)//顶级节点
				{
					
					org=<orgitem />;
					org.@ORGNAME = obj.ORGNAME;        
					org.@PARENTORGID =obj.PARENTORGID;
					org.@ORGTYPEID=obj.ORGTYPEID;
					org.@ORGID =obj.ORGID;
					org.@TOPORGID=obj.TOPORGID;
					org.@OEGSEQ=obj.ORGSEQ;
					
				}
				else
				{ //07 代表岗位
					if(!flag&&obj.ORGTYPEID=='07' ){
						continue;
					}
					treenode=org.(@ORGID==obj.PARENTORGID);
					if(	treenode.length()>0)
					{
						treenode[0].appendChild(createNewItem(obj));
						continue;
					}
					treenode=org..*.(@ORGID==obj.PARENTORGID);
					if(treenode.length()>0)
					{
						treenode[0].appendChild(createNewItem(obj));
					}
				}
				
			}
			return org;
		}
		/**
		 * 退出当前模块
		 * @param displayObj  this
		 * @param curModuleName 模块url
		 */
		public static function exitCurModule(displayObj:Sprite,curModuleName:String):void{
			if(!(displayObj.parent.parent is SuperTabNavigator))return;
			var stn:SuperTabNavigator=displayObj.parent.parent as SuperTabNavigator;
			var suba:Array=stn.getChildren();
			try
			{
				
				for (var i:int=0; i < suba.length; i++)
				{
					if (suba[i] is ModuleLoader)
					{
						var o:ModuleLoader=suba[i] as ModuleLoader;
						if (o.url ==curModuleName )
						{
							suba[i].removeAllChildren();
							
							stn.data[o.name]=null;
							stn.removeChild(suba[i]);
							o.url='';
							//suba[i].removeAllChildren();
							//suba[i].unloadModule();
							break;
						}
					}else if(suba[i] is FlexModuleLoader){
						var om:FlexModuleLoader=suba[i] as FlexModuleLoader;
						stn.data[om.name]=null;
						om.removeAllChildren();
						
						om.dispose();
						
						stn.removeChild(om);
						//suba[i].removeAllChildren();
						//suba[i].unloadModule();
						break;
					}
				}
				
			}
			catch (e)
			{
				
			}
			
			
		}
		private static function showLoad():void {
			group = new Group();
			load = new preLoading();
			group.addElement(load);
			Ct.popup.ct_popupWindow(group, FlexGlobals.topLevelApplication.box);
		}
		
		
	}
}